# Maintainer: Jeroen Ooms <jeroen@berkeley.edu>

_realname=aws-sdk-cpp
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.8.133
# Sync these versions with aws-sdk-cpp/third-party/CMakeLists.txt
_aws_common_ver=0.4.42
_aws_checksums_ver=0.1.5
_aws_stream_ver=0.1.5
pkgrel=3
pkgdesc="AWS SDK for C++ (mingw-w64)"
arch=('any')
url="https://github.com/aws/aws-sdk-cpp"
license=('Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
options=('strip' 'staticlibs')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/aws/aws-sdk-cpp/archive/${pkgver}.tar.gz"
        "AwsCCommon.tar.gz"::"https://github.com/awslabs/aws-c-common/archive/v${_aws_common_ver}.tar.gz"
        "AwsChecksums.tar.gz"::"https://github.com/awslabs/aws-checksums/archive/v${_aws_checksums_ver}.tar.gz"
        "AwsCEventStream.tar.gz"::"https://github.com/awslabs/aws-c-event-stream/archive/v${_aws_stream_ver}.tar.gz"
        "aws-sdk-cpp-pr-1333.patch"
        "0001-cmake-change-deps-dir.patch"
        "0002-third-party-cmake-remove-git.patch"
        "BuildAwsCCommon.patch"
        "BuildAwsChecksums.patch")
noextract=("AwsCCommon.tar.gz"
           "AwsChecksums.tar.gz"
           "AwsCEventStream.tar.gz")
sha256sums=('d6c495bc06be5e21dac716571305d77437e7cfd62a2226b8fe48d9ab5785a8d6'
            'a24f9918fad1970e1320ece3c95684c7aeb71250f67b3a87b3063cb3c801cff2'
            '6e6bed6f75cf54006b6bafb01b3b96df19605572131a2260fddaf0e87949ced0'
            'f1b423a487b5d6dca118bfc0d0c6cc596dc476b282258a3228e73a8f730422d4'
            '6d923d6eb72213bdfe44c6b41444be1817fbe30c5ef21bdd7c94200d417539f3'
            '9290f7fc1e31073144a0eaaa3ed6658f33af8e54c8a322344886dac098bf64b4'
            '5a3fb026cdbb50d9bc6c2fd3d333469eb55e33e8952c0d123435cbe61e94408f'
            '957eb1cc68b151622b7667e6a13506c96fdef8f566a227ac1608c9e02d3826d3'
            '20359b6e79430d1deb69bb05214a2f18258cb54ca56e15cd24605ad0c0094df1')

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/aws-sdk-cpp-pr-1333.patch"

  # Set dependencies search to current source directory
  patch -p1 -i "${srcdir}/0001-cmake-change-deps-dir.patch"

  # Replace git command to use our extracted tarball
  patch -p1 -i "${srcdir}/0002-third-party-cmake-remove-git.patch"

  # Extract and patch AWS related dependecies

  tar -xf "${srcdir}/AwsCCommon.tar.gz" || true
  mv "aws-c-common-${_aws_common_ver}" AwsCCommon
  patch -d AwsCCommon -Np1 -i "${srcdir}/BuildAwsCCommon.patch"

  tar -xf "${srcdir}/AwsChecksums.tar.gz" || true
  mv "aws-checksums-${_aws_checksums_ver}" AwsChecksums
  patch -d AwsChecksums -Np1 -i "${srcdir}/BuildAwsChecksums.patch"

  tar -xf "${srcdir}/AwsCEventStream.tar.gz" || true
  mv "aws-c-event-stream-${_aws_stream_ver}" AwsCEventStream
}

build() {
  [[ -d "${srcdir}"/build-${CARCH}-static ]] && rm -rf "${srcdir}"/build-${CARCH}-static
  mkdir -p "${srcdir}"/build-${CARCH}-static && cd "${srcdir}"/build-${CARCH}-static

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -GNinja \
      -DCMAKE_BUILD_TYPE=Release \
      -DBUILD_SHARED_LIBS=OFF \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DBUILD_ONLY="config;identity-management;s3;sts;transfer" \
      -DENABLE_UNITY_BUILD=ON \
      -DAUTORUN_UNIT_TESTS=OFF \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}"/build-${CARCH}-static
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
